package com.atguigu.yygh.user.api;

import com.atguigu.yygh.common.model.user.Patient;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 就诊人管理接口
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Resource
    private PatientService patientService;

    //获取就诊人列表
    @GetMapping("auth/findAll")
    public Result<List<Patient>> findAll(HttpServletRequest request){

        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        //根据userId获取当前用户添加的就诊人列表
       List<Patient> patients =  patientService.findAllUserId(userId);
       return Result.ok(patients);
    }
    //添加就诊人
    @PostMapping("auth/save")
    public Result<Void> savePatient(@RequestBody Patient patient,HttpServletRequest request){
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    //根据id获取就诊人信息
    @GetMapping("auth/get/{id}")
    public Result<Patient> getPatient(@PathVariable Long id){
       Patient patient =  patientService.getPatientId(id);
       return Result.ok(patient);
    }
    //修改就诊人
    @PostMapping("auth/update")
    public Result<Void> updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }
    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public Result<Void> removePatient(@PathVariable Long id){

        patientService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Result<Patient> getPatientOrder(@PathVariable("id") Long id){
        return Result.ok(patientService.getPatientId(id));
    }
}
